import { objDeepCopy,getDateString } from "@/assets/js/common.js";
import commonObject from "@/assets/js/xm.js";
let Controller = {
	activated() {
		this.init();
	},
	mounted() {
		this.init();
	},
	methods: {
		init() {
			this.getMainList(this);
			this.getCardMsg(this,1);// 卡片
			this.getStatusSelect(this,'orderStatus',1)// 订单状态
			this.getStatusSelect(this,'payStatus',9)// 支付状态
			this.getStatusSelect(this,'orderAfterSaleStatus',10)// 售中售后状态
			
			this.getSupplierShop(this, undefined, 4) //发货门店
		},
		//获取卡片信息
		getCardMsg(that,type){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url: apiTest + '/sale/saleOrder/statistics/' + type,
			}).then((res) => {
				var data = res.data;
				that.Model.configArr = [];
				if (data.status == 200) {
					var result = data.result;
					var obj;
					
					for (let i = 0; i < result.length; i++) {
						obj = {
							value:result[i].tabIndex,
							color:result[i].color,
							label:result[i].tabName,
							count:result[i].total,
							attributeValue:result[i].attributeValue,
							attributeName:result[i].attributeName,
						}
						that.Model.configArr.push(obj);
					}
					
				} else {
					that.tips(that, 'warning', data.msg);
				}
			
			});
		},
		//获取 订单状态and支付状态 售中售后状态  下拉框
		getStatusSelect(that,type,index){
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/base/systemDictionary/getByTypeToMap/' + type,
				data:{}
			}).then((res) => {
				var data = res.data;
				// that.Model.configArr = [];
				if (data.status == 200) {
					var result = data.result;
					var obj;
					var arr=[]
					Object.keys(result).forEach(function(key){
						// console.log(key,result[key]);
						obj = {
							label: result[key].value,
							value: result[key].key
						}
						arr.push(obj)
					});
					
					that.Model.filterOption.items[index].data = objDeepCopy(arr);
					
				} else {
					that.tips(that, 'warning', data.msg);
				}
			
			});
		},
		//获取 门店下拉框
		getSupplierShop(that, city, index) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'post',
				url: apiTest + '/supplier/supplierShop/getList',
				data: {
					city: city
				}
			}).then((res) => {
				var data = res.data;
				if (data.status == 200) {
					that.Model.deliverStores = [];  //发货门店
					var result = data.result;
					if (result) {
						var obj;
						var arr = [];
						for (let i = 0; i < result.length; i++) {
							obj = {
								label:result[i].theNameOfTheStore,
								value:result[i].theFactory
							}
							arr.push(obj);
						}
						that.Model.filterOption.items[index].data = objDeepCopy(arr);
					}
		
				} else {
					that.tips(that, 'warning', data.msg)
				}
		
			});
		},
			
		//点击卡片
		cardEmit(card){
			// let params = objDeepCopy(this.Model.searchFromData);
			// params.tabIndex = card.value;
			//点击卡片 清空下面的搜索条件，并将状态的下拉框对应
			var params = {
				tabIndex:card.value
			}
			this.getMainList(this,params)
			//退款中的状态 （写死）
			if(card.attributeName == 'orderStatus'){
				if(card.attributeValue == null){
					this.Model.filterOption.items[1].value = '';
				}else{
					this.Model.filterOption.items[1].value = String(card.attributeValue);
				}
			}
		},
		getMainList(that,params,pageIndex = 1,pageSize = 10) {
		  var apiTest = this.apiTest;
		  params = params || {tabIndex:10};
		  this.$ajax({
				method: 'post',
				url:apiTest+'/sale/saleOrder/list',
				data:{
					condition:params,
					pageIndex: pageIndex,
					pageSize: pageSize,
				}
			  }).then((res)=>{
				that.Model.tableData = [];
				var data = res.data;
				if(data.status == 200){
					var obj = {};
					var pagination = commonObject.paginationFunction(data.result);
					that.Model.paginationObject = objDeepCopy(pagination);
					var result = data.result.result;
					console.log(data.result);
					for (var i = 0; i < result.length; i++) {
						obj = objDeepCopy(result[i]);
						var showPay;
						switch (result[i].payStatus){
							case 0:showPay = '待支付'
								break;
							case 1:showPay = '部分支付'
								break;
							case 2:showPay = '已支付'
								break;
							default:
								break;
						}
						obj.showPay = showPay;
						obj.payRatio = obj.payRatio+'%';
						//发货方式
						var showMethod;
						switch (result[i].shippingMethod){
							case 0:showMethod = '快递'
								break;
							case 1:showMethod = '自提'
								break;
							default:
								break;
						}
						obj.showMethod = showMethod;
								
						//订单状态
						var showOrderStatus;
						switch (result[i].orderStatus){
							case 0:showOrderStatus = '待付款'
								break;
							case 1:showOrderStatus = '待审核'
								break;
							case 2:showOrderStatus = '待发货'
								break;
							case 3:showOrderStatus = '待提货'
								break;
							case 4:showOrderStatus = '部分已发货'
								break;
							case 5:showOrderStatus = '待收货'
								break;
							case 6:showOrderStatus = '已完成'
								break;
							case 7:showOrderStatus = '已关闭'
								break;
							default:
								break;
						}
						obj.showOrderStatus = showOrderStatus;
						//订单类型
						var showorderType;
						switch (result[i].orderType){
							case 10:showorderType = '现货订单'
								break;
							case 20:showorderType = '定制订单'
								break;
							case 30:showorderType = '售中订单'
								break;
							case 40:showorderType = '售后订单'
								break;
							default:
								break;
						}
						obj.showorderType = showorderType;
						
						that.Model.tableData.push(obj);
					}
	  
				}else{
	  
				}
	  
	  
	  
			});
		},
		//订单核销
		onOrderConfirm(that,id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url:apiTest+'/sale/saleOrder/confirm/'+id,
			}).then((res)=>{
				console.log(res);
				var data = res.data;
				if(data.status == 200){
					that.getMainList(that);
					that.tips(that,'success','操作成功');
				}else{
					that.tips(that,'warning',data.msg);
				}
			});
		},
		//取消订单
		onOrderCancel(that,id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url:apiTest+'/sale/saleOrder/cancel/'+id,
			}).then((res)=>{
				console.log(res);
				var data = res.data;
				if(data.status == 200){
					that.getMainList(that)
					that.tips(that,'success','操作成功');
				}else{
					that.tips(that,'warning',data.msg);
				}
			});
		},
		//发货
		onOrderSend(that,id) {
			var apiTest = this.apiTest;
			this.$ajax({
				method: 'get',
				url:apiTest+'/sale/saleOrder/send/'+id,
			}).then((res)=>{
				console.log(res);
				var data = res.data;
				if(data.status == 200){
					that.getMainList(that)
					that.tips(that,'success','操作成功');
				}else{
					that.tips(that,'warning',data.msg);
				}
			});
		},
		//
		
		
		//点击 支付
		orderPay(row) {
			this.Model.paySaleOrderId = row.id;
			this.Model.dialogFormVisible = true;
		},
		//确认支付
		comfirmPay() {
			var that = this;
			var apiTest = this.apiTest;
			var params = this.Model.dialogForm;
			params.createTime = getDateString(params.createTime);
			
			params.saleOrderId = this.Model.paySaleOrderId;
			this.$ajax({
				method: 'post',
				url:apiTest+'/sale/saleOrderPay',
				data:params
			}).then((res)=>{
				console.log(res);
				var data = res.data;
				if(data.status == 200){
					that.getMainList(that);
					that.tips(that,'success','操作成功');
					setTimeout(function(){
						that.Model.dialogFormVisible = false;
					},1000)
				}else{
					that.tips(that,'warning',data.msg);
				}
			});
		},
		//点击 核销，取消订单，发货
		changeOrderStatus(row,type) {
			var that = this;
			this.$confirm('是否确认'+type+'?', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
			}).then(() => {
				if(type == '核销'){
					that.onOrderConfirm(that,row.id)
				}else if(type == '取消订单'){
					that.onOrderCancel(that,row.id)
				}else{
					that.onOrderSend(that,row.id)
				}
			}).catch(() => {
			  
			});	
		},
		toDetial(row) {
			let href;
			let name;
			switch (row.orderType) {
				//现货
				case 10:
					href = 'spotOrderDetail'; 
					break;
				//定制	
				case 20:
					href = 'customizeOrderDetail'; 
					break;
				//售中	
				case 30:
					switch (row.afterSaleType) {
						/* （10售中变更 20退款 30升级换款 40常规售后） */
						case 10:
							name = 'orderDetailChange'; //售中变更-跳变更页
							break;
						case 20:
							name = 'orderDetailRefund'; //退款-跳退款页
							break;
						default:
							break;
					}
				case 40:
					switch (row.afterSaleType) {
						/* （30售后升级换款 40常规售后 50售后退款） */
						//30跳变更,50跳退款
						case 30:
							name = 'afterOrderDetailChange';
							break;
						case 40:
							name = 'afterOrderDetailChange';
							break;
						case 50:
							name = 'afterOrderDetailRefund';
							break;
						default:
							break;
					}
					break;	
				default:
					break;	
			}
			if (href) {
				this.$router.push({
					name: href,
					query: {
						saleNumber:row.saleNumber,
						id:row.id
					}
				})
			}
			if (name) {
				this.$router.push({
					name: name,
					query: {
						data: row
					}
				})
			}
			
			
		},
		selectedHighlight(data) {
			// 如果选中则添加高亮样式
			if (this.Model.TableSelectData.indexOf(data.row) > -1) {
				return {
					"background-color": "#FEF6EE"
				};
				return {}
			}
		},
		handleSelectionChange(val) {
			this.Model.TableSelectData = val;
		},
		handleCurrentChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getMainList(this,searchFromData,val);
		},
		handleSizeChange(val) {
			var searchFromData = this.Model.searchFromData;
			this.getMainList(this,searchFromData,undefined,val);
		},
		showChild(val) {
			let obj = val;
			if(val.creatTime){
				obj.bookOrderStartTime = getDateString(val.creatTime[0]);
				obj.bookOrderEndTime = getDateString(val.creatTime[1]);
				val.creatTime = undefined;
            }
            switch (val.orderStatus){
                case '0':obj.tabIndex = 10;
                    break;
                case '1':obj.tabIndex = 10;
                    break;
                case '2':obj.tabIndex = 11;
                    break;
                case '3':obj.tabIndex = 12;
                    break;
                case '4':obj.tabIndex = 10;
                    break;
                case '5':obj.tabIndex = 10;
                    break;
                case '6':obj.tabIndex = 10;
                    break;
                case '7':obj.tabIndex = 10;
                    break;
                default:obj.tabIndex = 10;
                    break;
            }
			this.Model.searchFromData = objDeepCopy(obj);
		},
		toSearch(val) {
			var params = this.Model.searchFromData;
			this.getMainList(this, params);
		},
		toClear(val) {
			this.Model.searchFromData = this.Model.defaultSearchFromData;
		},
		toExport(val) {
			console.log(val);
		},
		onTabChange(val) {
			console.log(val, this.Model.activeTab)
			switch (val) {
				case 1:
					break;
				case 2:
					break;
				case 3:
					break;
			}
		},
		tips(that,type,message) {
			that.$message({
				message: message,
				type: type
			});
		},
	}
}
export default Controller;
